package com.ssm.juc.synchronized1;

/**
 * 锁失效
 */
public class InvalidLockTest {

    static int i = 0;

    public static void main(String[] args) throws InterruptedException {
        // 锁异常1：共享对象为null
//        Object lockObj;
//        synchronized (lockObj) {
//            System.out.println(1);
//        }

        //锁异常2: 共享对象不同
        InvalidLockTest test1 = new InvalidLockTest();
        InvalidLockTest test2 = new InvalidLockTest();

        Thread thread1 = new Thread(() -> {
            for (int j = 0; j < 10000; j++) {
                test1.add();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int j = 0; j < 10000; j++) {
                test1.add();
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("i=" + i);
    }

    public synchronized void add() {
        i++;
    }
}